package com.sky.builder;

/**
 * @author : wushikai
 * <p>
 * date : 2020-08-20
 */

/**建造者实例    构造computer */
public class Computer {


    private String cpu;
    private String screen;
    private String memory;
    private String motherBoard;

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getScreen() {
        return screen;
    }

    public void setScreen(String screen) {
        this.screen = screen;
    }

    public String getMemory() {
        return memory;
    }

    public void setMemory(String memory) {
        this.memory = memory;
    }

    public String getMotherBoard() {
        return motherBoard;
    }

    public void setMotherBoard(String motherBoard) {
        this.motherBoard = motherBoard;
    }

    public Computer(String cpu, String screen, String memory, String motherBoard) {
        this.cpu = cpu;
        this.screen = screen;
        this.memory = memory;
        this.motherBoard = motherBoard;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", screen='" + screen + '\'' +
                ", memory='" + memory + '\'' +
                ", motherBoard='" + motherBoard + '\'' +
                '}';
    }
}



class ComputerBuilder{

    private String cpu;
    private String screen;
    private String memory;
    private String motherBoard;

    private ComputerBuilder(Builder builder) {
        cpu = builder.cpu;
        screen = builder.screen;
        memory = builder.memory;
        motherBoard = builder.motherBoard;
    }

    public  static final class Builder {

        private String cpu;
        private String screen;
        private String memory;
        private String motherBoard;

        public Builder() {
            this.cpu = "英特尔";
            this.screen = "戴尔";
            this.memory = "金士顿";
            this.motherBoard = "联想";
        }


        public Builder buildCpu(String val){
            cpu = val;
            return this;
        }

        public Builder buildScreen(String val) {
            screen = val;
            return this;
        }
        public Builder buildMemory(String val) {
            memory = val;
            return this;
        }

        public Builder buildMotherBoard(String val) {
            motherBoard = val;
            return this;
        }

        public ComputerBuilder build(){

            return new ComputerBuilder(this);
        }


    }

    @Override
    public String toString() {
        return "ComputerBuilder{" +
                "cpu='" + cpu + '\'' +
                ", screen='" + screen + '\'' +
                ", memory='" + memory + '\'' +
                ", motherBoard='" + motherBoard + '\'' +
                '}';
    }
}


class Client{

    public static void main(String[] args){
        /* 不使用建造者模式 */
        Computer computer = new Computer("英特尔", "三星", "美光", "微星");
        System.out.println(computer.toString());

        /*使用建造者模式*/
        ComputerBuilder computerBuilder = new ComputerBuilder.Builder().buildCpu("amd").buildMemory("镁光").build();
        System.out.println(computerBuilder.toString());


    }

}
